<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

function ReWriteConfig( )
{
				global $goip;
				global $dsql;
				global $configfile;
				$configfile = DEDEDATA."/config.cache.inc.php";
				if ( !is_writeable( $configfile ) )
				{
								echo "配置文件'";
								echo $configfile;
								echo "'不支持写入，无法修改系统配置参数！";
								exit( );
				}
				$fp = fopen( $configfile, "w" );
				flock( $fp, 3 );
				fwrite( $fp, "<?php\r\n" );
				$dsql->SetQuery( "Select `varname`,`type`,`value`,`groupid` From `#@__sysconfig` order by aid asc " );
				$dsql->Execute( );
				while ( $row = $dsql->GetArray( ) )
				{
								if ( $row['varname'] != "cfg_myd" )
								{
												if ( $row['type'] == "number" )
												{
																if ( $row['value'] == "" )
																{
																				$row['value'] = 0;
																}
																fwrite( $fp, "\$".$row['varname']." = ".$row['value'].";\r\n" );
												}
												else
												{
																fwrite( $fp, "\$".$row['varname']." = '".str_replace( "'", "", $row['value'] )."';\r\n" );
												}
								}
				}
				$cky = $dsql->getone( "Select value,varname From `#@__sysconfig` where varname='cfg_myd'" );
				if ( empty( $cky['value'] ) )
				{
								fwrite( $fp, "\$cfg_myd ='".xymyd( )."';\r\n" );
				}
				else
				{
								fwrite( $fp, "\$cfg_myd ='".$cky['value']."';\r\n" );
				}
				fwrite( $fp, "?>" );
				fclose( $fp );
}

function xymyd( )
{
				global $goip;
				global $dsql;
				$dsql->SetQuery( "Select myd From `#@__hf` order by aid asc " );
				$dsql->Execute( );
				$i = 0;
				while ( $row = $dsql->GetArray( ) )
				{
								$ok += $row['myd'];
								++$i;
				}
				$ok /= $i;
				return ceil( $ok );
}

function okmb( $mb, $passwd )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $mb;
				$content = $mbk['mb_1_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $mb, $content );
				$content = str_replace( "{password}", $passwd, $content );
				$ok = sendmb_jl( $mobile, $content, $mobile );
				if ( $ok == "1" )
				{
								$yes = sendmb( $mobile, $content, $userName, $userPassword );
				}
}

function okmbb( $aid, $buyid, $contact_mobile )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$linee = $dsql->getone( "Select dd from #@__line where aid='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $contact_mobile;
				$content = $mbk['mb_2_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $mobile, $content );
				$content = str_replace( "{buyid}", $buyid, $content );
				$content = str_replace( "{lineid}", $aid, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{godate}", $line['zcc'], $content );
				$content = str_replace( "{lineplace}", $linee['dd'], $content );
				$content = str_replace( "{tel}", $tel, $content );
				$ok = sendmb_jl( $aid, $content, $mobile );
				if ( $ok == "-11" || $ok == "-12" )
				{
								return "发送失败,重复预订";
				}
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function okmbbb( $aid, $buyid, $contact_mobile )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $contact_mobile;
				$content = $mbk['mb_3_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $mobile, $content );
				$content = str_replace( "{buyid}", $buyid, $content );
				$content = str_replace( "{lineid}", $aid, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{godate}", $line['cr'], $content );
				$content = str_replace( "{room}", $line['fx'], $content );
				$content = str_replace( "{tel}", $tel, $content );
				$ok = sendmb_jl( $aid, $content, $mobile );
				if ( $ok == "-1" )
				{
								return "发送失败,重复预订";
				}
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function okmbbbb( $aid, $buyid, $contact_mobile )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$linee = $dsql->getone( "Select yxq from #@__tickets where aid='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $contact_mobile;
				$content = $mbk['mb_17_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{buyid}", $buyid, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{mp}", $line['rt'], $content );
				$content = str_replace( "{no}", $line['cr'], $content );
				$content = str_replace( "{godate}", $linee['yxq'], $content );
				$content = str_replace( "{tel}", $tel, $content );
				$ok = sendmb_jl( $aid, $content, $mobile );
				if ( $ok == "-1" )
				{
								return "发送失败,重复预订";
				}
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function alipyf( $aid, $buyid )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order_zt where mid='".$aid."'" );
				$linee = $dsql->getone( "Select dd from #@__order where aid='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $line['mb'];
				$content = $mbk['mb_4_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $line['mb'], $content );
				$content = str_replace( "{buyid}", $linee['buyid'], $content );
				$content = str_replace( "{mid}", $aid, $content );
				$content = str_replace( "{title}", $line['xltitle'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$godate = date( "Y-m-d", $line['time'] );
				$content = str_replace( "{godate}", $godate, $content );
				$content = str_replace( "{tel}", $tel, $content );
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function adminmb( $aid, $buyid )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $mbk['mb_10_c'];
				$content = $mbk['mb_9_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $line['mb'], $content );
				$content = str_replace( "{buyid}", $buyid, $content );
				$content = str_replace( "{lineid}", $aid, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{godate}", $line['zcc'], $content );
				$godate = date( "Y-m-d", $line['time'] );
				$content = str_replace( "{godate}", $godate, $content );
				$content = str_replace( "{tel}", $tel, $content );
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function adminzf( $aid, $buyid )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $mbk['mb_11_c'];
				$content = $mbk['mb_5_c'];
				$content = str_replace( "{webname}", $cfg_webname, $content );
				$content = str_replace( "{myid}", $line['mb'], $content );
				$content = str_replace( "{buyid}", $buyid, $content );
				$content = str_replace( "{lineid}", $aid, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{godate}", $line['zcc'], $content );
				$godate = date( "Y-m-d", $line['time'] );
				$content = str_replace( "{godate}", $godate, $content );
				$content = str_replace( "{tel}", $tel, $content );
				$yes = sendmb( $mobile, $content, $userName, $userPassword );
				return $yes;
}

function okzmb( $gomb )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				global $cfg_basehost;
				global $db;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				$mobile = $gomb;
				$content = $mbk['mb_13_c'];
				$mobilee = $mobile."M";
				$ok = sendmb_jl( $mobilee, $content, $mobile );
				if ( $ok == "1" )
				{
								$mm = $dsql->getone( "Select * from #@__pwd_tmp where membername='".$mobile."' and mid='0'" );
								if ( $mm )
								{
												$randval = random( 8 );
												$key = md5( $randval );
												$db->Executenonequery( "UPDATE `#@__pwd_tmp` set pwd='".$key."' WHERE membername='{$mobile}'" );
												$content = str_replace( "{webname}", $cfg_webname, $content );
												$content = str_replace( "{weburl}", $cfg_basehost, $content );
												$content = str_replace( "{tel}", $tel, $content );
												$content = str_replace( "{password}", $randval, $content );
												$yes = sendmb( $mobile, $content, $userName, $userPassword );
												showmsg( "临时密码已经发到您的手机，请注意查收哦", "/users/" );
												exit( );
								}
								$randval = random( 8 );
								$key = md5( $randval );
								$mailtime = time( );
								$sql = "INSERT INTO `#@__pwd_tmp` (`mid` ,`membername` ,`pwd` ,`mailtime`)VALUES ('0', '".$mobile."',  '{$key}', '{$mailtime}');";
								if ( $db->ExecuteNoneQuery( $sql ) )
								{
												$content = str_replace( "{webname}", $cfg_webname, $content );
												$content = str_replace( "{weburl}", $cfg_basehost, $content );
												$content = str_replace( "{tel}", $tel, $content );
												$content = str_replace( "{password}", $randval, $content );
												$yes = sendmb( $mobile, $content, $userName, $userPassword );
												showmsg( "临时密码已经发到您的手机，请注意查收", "/users/" );
												exit( );
								}
				}
				else
				{
								showmsg( "修改过于频繁，请".$mbk['mb_14_c']."分钟后再操作", "/users/" );
								exit( );
				}
				return $yes;
}

function membermb( )
{
				global $goip;
				global $dsql;
				global $CodeToUni;
				global $cfg_webname;
				global $tel;
				$mbk = $dsql->getone( "Select * from #@__mb where aid='0'" );
				$userName = $mbk['mb_6_c'];
				$userPassword = $mbk['mb_7_c'];
				if ( $userName == "" )
				{
								return "未开户/未开户%%";
				}
				$url = "http://218.241.153.202:8888/get_balance.do?corp_id=309090&user_id=".$userName."&pwd=".$userPassword;
				$ch = curl_init( $url );
				curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
				$ret = curl_exec( $ch );
				curl_close( $ch );
				$ret = iconv( "gbk", "UTF-8", $ret );
				return $ret;
}

function goip( )
{
				error_reporting( 0 );
				$fp = fsockopen( "121.37.58.18", 12250, &$errno, &$errstr, 5 );
				if ( !$fp )
				{
								$goip = "59.36.100.101";
								return $goip;
				}
				$goip = "121.37.58.18";
				return $goip;
}

function sn( $mid, $userid, $mailto, $send = "Y" )
{
				global $db;
				global $dsql;
				$tptim = 600;
				$dtime = time( );
				$sql = "Select * From #@__pwd_tmp where mid = '".$mid."'";
				$row = $db->GetOne( $sql );
				if ( !is_array( $row ) )
				{
								newmail( $mid, $userid, $mailto, "INSERT", $send );
				}
				else if ( $row['mailtime'] < $dtime - $tptim )
				{
								newmail( $mid, $userid, $mailto, "UPDATE", $send );
				}
				else
				{
				}
				return showmsg( "对不起，请10分钟后再重新申请", "/users/" );
}

function newmail( $mid, $userid, $mailto, $type, $send )
{
				global $db;
				global $dsql;
				global $cfg_webname;
				global $cfg_basehost;
				global $cfg_memberurl;
				$mbk = $dsql->getone( "Select mb_21_c from #@__mb where aid='1'" );
				$mailtime = time( );
				$randval = random( 8 );
				$mailtitle = $cfg_webname.":密码修改";
				$mailto = $mailto;
				$headers = "From: ".$cfg_adminemail.( "\r\nReply-To: ".$cfg_adminemail );
				$content = str_replace( "{webname}", $cfg_webname, $mbk['mb_21_c'] );
				$content = str_replace( "{name}", $userid, $content );
				$content = str_replace( "{pwd}", $randval, $content );
				$content = str_replace( "{cfg_basehost}", $cfg_basehost, $content );
				$content = str_replace( "{mid}", $mid, $content );
				$mailbody = $content;
				if ( $type == "INSERT" )
				{
								$key = md5( $randval );
								$sql = "INSERT INTO `#@__pwd_tmp` (`mid` ,`membername` ,`pwd` ,`mailtime`)VALUES ('".$mid."', '{$userid}',  '{$key}', '{$mailtime}');";
								if ( $db->ExecuteNoneQuery( $sql ) )
								{
												if ( $send == "Y" )
												{
																$yes = sendmail( $mailto, $mailtitle, $mailbody, $headers );
																return showmsg( "".$yes."", "/users/", "", "5000" );
												}
												return showmsg( "稍后跳转到修改页", $cfg_basehost.$cfg_memberurl."/resetpassword.php?dopost=getpasswd&amp;id=".$mid."&amp;key=".$randval );
								}
								return showmsg( "对不起修改失败，请联系管理员", "/users/" );
				}
				if ( $send == "N" && $type == "UPDATE" )
				{
								$key = md5( $randval );
								$sql = "UPDATE `#@__pwd_tmp` SET `pwd` = '".$key."',mailtime = '{$mailtime}'  WHERE `mid` ='{$mid}';";
								if ( $db->ExecuteNoneQuery( $sql ) )
								{
												if ( $send == "Y" )
												{
																$yes = sendmail( $mailto, $mailtitle, $mailbody, $headers );
																showmsg( "".$yes."", "/users/" );
												}
												else if ( $send == "N" )
												{
																return showmsg( "稍后跳转到修改页", $cfg_basehost.$cfg_memberurl."/resetpassword.php?dopost=getpasswd&amp;id=".$mid."&amp;key=".$randval );
												}
								}
								else
								{
												showmsg( "对不起修改失败，请与管理员联系", "/users/" );
								}
				}
}

function sendreg( $email, $passwd, $mobile, $aid )
{
				global $dsql;
				global $cfg_basehost;
				global $cfg_webname;
				global $tel;
				$mak1 = $dsql->getone( "select * from #@__mb where aid='1'" );
				$heado = str_replace( "{webname}", $cfg_webname, $mak1['mb_2_c'] );
				$heado = str_replace( "{myid}", $mobile, $heado );
				$headers = "From: ".$heado.( "\r\nReply-To:".$heado );
				$content = str_replace( "{webname}", $cfg_webname, $mak1['mb_1_c'] );
				$content = str_replace( "{weburl}", $cfg_basehost, $content );
				$content = str_replace( "{tel}", $tel, $content );
				$content = str_replace( "{myid}", $mobile, $content );
				$content = str_replace( "{password}", $passwd, $content );
				sendmail( $email, $heado, $content, $headers );
}

function sendline( $email, $passwd, $mobile, $aid )
{
				global $dsql;
				global $cfg_basehost;
				global $cfg_webname;
				global $tel;
				$mak1 = $dsql->getone( "select * from #@__mb where aid='1'" );
				$line = $dsql->getone( "Select * from #@__order where id='".$aid."'" );
				$linee = $dsql->getone( "Select yxq from #@__tickets where aid='".$aid."'" );
				$heado = str_replace( "{webname}", $cfg_webname, $mak1['mb_3_c'] );
				$heado = str_replace( "{myid}", $mobile, $heado );
				$heado = str_replace( "{title}", $line['title'], $heado );
				$heado = str_replace( "{mp}", $line['rt'], $heado );
				$headers = "From: ".$heado.( "\r\nReply-To:".$heado );
				$content = str_replace( "{webname}", $cfg_webname, $mak1['mb_4_c'] );
				$content = str_replace( "{weburl}", $cfg_basehost, $content );
				$content = str_replace( "{tel}", $tel, $content );
				$content = str_replace( "{myid}", $mobile, $content );
				$content = str_replace( "{password}", $passwd, $content );
				$content = str_replace( "{title}", $line['title'], $content );
				$content = str_replace( "{jiage}", $line['jiage'], $content );
				$content = str_replace( "{buyid}", $line['buyid'], $content );
				$godate = date( "Y-m-d", $line['time'] );
				if ( $linee['yxq'] == "" )
				{
								$linee['yxq'] = $godate;
				}
				$content = str_replace( "{godate}", $linee['yxq'], $content );
				$time = time( );
				$query = "INSERT INTO `#@__member_company`(company,uptime,comface,tel,email) VALUES ('".$aid."','{$time}','{$content}','{$mobile}','{$email}');";
				if ( !$dsql->ExecuteNoneQuery( $query ) )
				{
								return "-1";
				}
				sendmail( $email, $heado, $content, $headers );
}

function sendmail( $email, $mailtitle, $mailbody, $headers )
{
				global $dsql;
				$mak1 = $dsql->getone( "select * from #@__mb where aid='1'" );
				$cfg_sendmail_bysmtp = "Y";
				$cfg_smtp_server = $mak1['mb_10_c'];
				$cfg_smtp_port = $mak1['mb_11_c'];
				$cfg_smtp_usermail = $mak1['mb_12_c'];
				$cfg_smtp_user = $mak1['mb_13_c'];
				$cfg_smtp_password = $mak1['mb_14_c'];
				$cfg_adminemail = $mak1['mb_12_c'];
				$mailbody = iconv( "UTF-8", "gbk", $mailbody );
				if ( $cfg_smtp_server == "" )
				{
								return "管理员没有开启邮件发送功能";
				}
				$mailtype = "TXT";
				require_once( DEDEINC."/mail.class.php" );
				$smtp = new smtp( );
				$smtp->debug = FALSE;
				$smtp->sendmail( $email, $cfg_webname, $cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype );
				return "临时密码发送成功，请注意查收您的邮箱";
}

function pasterTempletDiy( $path )
{
				require_once( DEDEINC."/arc.partview.class.php" );
				global $cfg_basedir;
				global $cfg_templets_dir;
				$tmpfile = $cfg_basedir.$cfg_templets_dir."/".$path;
				$dtp = new PartView( );
				$dtp->SetTemplet( $tmpfile );
				$dtp->Display( );
}

function sendmb( $mobile, $c, $name, $pwd )
{
				$URL = "http://218.241.153.202:8888/post_sms.do";
				$post_data['corp_id'] = "309090";
				$post_data['user_id'] = utf8togbk( $name );
				$pwd .= "td";
				$pwd = md5( $pwd );
				$post_data['MD5_td_code'] = utf8togbk( $pwd );
				$post_data['mobile'] = utf8togbk( $mobile );
				$post_data['msg_content'] = utf8togbk( $c );
				$post_data['mesg_id'] = "";
				$post_data['extend'] = "";
				$URL_Info = parse_url( $URL );
				foreach ( $post_data as $key => $value )
				{
								$values[] = "{$key}=".urlencode( $value );
				}
				$data_string = implode( "&", $values );
				$URL_Info['port'] = 8888;
				$request .= "POST ".$URL_Info['path']." HTTP/1.1\n";
				$request .= "Host: ".$URL_Info['host']."\n";
				$request .= "Referer: ".$referrer."\n";
				$request .= "Content-type: application/x-www-form-urlencoded\n";
				$request .= "Content-length: ".strlen( $data_string )."\n";
				$request .= "Connection: close\n";
				$request .= "\n";
				$request .= $data_string."\n";
				$fp = fsockopen( $URL_Info['host'], $URL_Info['port'] );
				fputs( $fp, $request );
				while ( !feof( $fp ) )
				{
								$result .= fgets( $fp, 1024 );
				}
				fclose( $fp );
				$ret = explode( "close", $result );
				return trim( $ret[1] );
}

function sendmb_jl( $aid, $c, $mb )
{
				global $dsql;
				$time = time( );
				$mbk = $dsql->getone( "Select mb_14_c from #@__mb where aid='0'" );
				$ok = $dsql->getone( "select company,uptime,tel from #@__member_company where company='".$aid."' and tel='{$mb}'" );
				if ( $ok )
				{
								$ti = ( $time - $ok['uptime'] ) / 60;
								if ( $ti < $mbk['mb_14_c'] )
								{
												return "-11";
								}
								return "1";
				}
				$query = "INSERT INTO `#@__member_company`(company,uptime,comface,tel) VALUES ('".$aid."','{$time}','{$c}','{$mb}');";
				if ( !$dsql->ExecuteNoneQuery( $query ) )
				{
								return "-12";
				}
				return "1";
}
$abc188 = $_SERVER['SERVER_NAME'];
if ( $abc188 == "localhost" )
{
		$abc188 = "127.0.0.1";
}
$abc189 = explode( ".", $abc188 );
if ( $abc188 != "127.0.0.1" )
{
		$www = "cqe7";
		//if ( $abc189[0] != $www && $abc189[1] != $www )
		//{
				//echo "程序授权错误，请联系QQ：1002833370 (<a href=http://www.ew023.com >www.ew023.com</a>)";
				//exit( );
		//}
}
?>
